//常量

thispage=1;
totalpages=0;
delid=0;
//运行
roleshow(thispage);

//ajks
function roleshow(thispage){
	$.ajax({
		url:"/j/RoleControl/show",
		type:"post",
		dataType:"json",
		data:{
            thispage:thispage
        },
		success:function(data){

			workshow(data);
		},
		error:function (err){
		    console.log("错误");
		}
	})
}

//修改
function uproleshow(name,seq){
    $.ajax({
        url:"/j/RoleControl/update",
        type:"post",
        dataType:"json",
        data:{
            name:name,
            seq:seq,
        },
        success:function (data){

            roleshow(thispage);
        },
        error:function (err){
            console.log("错误");
        }
    })
}
//插入
function insertroleshow(name){
    $.ajax({
        url:"/j/RoleControl/insert",
        type:"post",
        dataType:"json",
        data:{
            name:name,
        },
        success:function (data){

            roleshow(thispage);
            document.getElementById("rolename").value="";
        },
        error:function (err){
            console.log("错误");
        }
    })
}
//删除
function deletroleshow(seq){
    $.ajax({
        url:"/j/RoleControl/delete",
        type:"post",
        dataType:"json",
        data:{
            seq:seq,
        },
        success:function (data){

            roleshow(thispage);
        },
        error:function (err){
            console.log("错误");
        }
    })
}
//查询多少个
function haveid(id){
    $.ajax({
        url:"haveid",
        type:"post",
        dataType:"json",
        data:{
            id
        },
        success:function (data){
            hvaidshow(data[0].num);
        },
        error:function (err){
            console.log("错误");
        }
    })
}
//响应
function workshow(data){
    var tbody =document.getElementById("tbody");
    var str='';
    for (var i=0;i<data.list.length;i++){
        var a=data.list[i];
        str +=`
<tr>
               <td>${a.backRole}</td>
               <td>${a.backName}</td>
               <td>
               <button class="btn btn-primary btn-sm" onclick="worktypeedict(this)">编辑</button>
               <button class="btn btn-danger btn-sm" onclick="backdelete(this)">删除</button>
               </td>
 </tr>
    `
        totalpages=(data.pages*5);
        var myPager = $('#myPager').data('zui.pager');
        myPager.set({
            page: thispage,
            recTotal: totalpages,
            recPerPage: 5
        });
    }
    tbody.innerHTML=str;
}

//onclick响应
function worktypeedict(thi){
    document.getElementById("text_name").value=thi.parentNode.parentNode.children[1].innerHTML;
    document.getElementById("exit").value=thi.parentNode.parentNode.children[0].innerHTML;
    $('#uprole').modal('toggle', 'center');
}

function roleupdate(){
    var name= document.getElementById("text_name").value;
    var seq = document.getElementById("exit").value;
    uproleshow(name,seq);
    $('#uprole').modal('hide', 'fit');
}

function backinsert(){
    var name=document.getElementById("rolename").value;
    insertroleshow(name);
    $('#mySmModal').modal('hide', 'fit');
}

function dsshow(){
    deletroleshow(delid);
    $('#deleteccj').modal('toggle', 'center');
}
function backdelete(thi){

    delid =thi.parentNode.parentNode.children[0].innerHTML;
    haveid(delid);
}
function hvaidshow(data){
    document.getElementById("ccjnumber").innerHTML="已有"+data+"用户使用该角色"+"您确定要删除吗？"
    $('#deleteccj').modal('toggle', 'fit');
}

// 分页
$('#myPager').on('onPageChange', function(e, state, oldState) {
    if (state.page !== oldState.page) {


        thispage=state.page;
        roleshow(state.page);
    }
});